using System;
using System.Windows.Forms;
using System.IO.Ports;

namespace WaterLeverMonitor
{
    public partial class Form2 : Form
    {
        private Form1 form1;
        private SerialPort serilport;
        public Form2(Form1 f)
        {
            InitializeComponent();
            form1 = f;

            serilport = form1.SerialPort;

            comboBox1.Text = "" + serilport.DataBits;
            comboBox2.Text = "" + serilport.DiscardNull;
            comboBox3.Text = "" + serilport.DtrEnable;
            comboBox4.Text = "" + serilport.Handshake;
            comboBox5.Text = "" + serilport.Parity;
            comboBox6.Text = "" + serilport.ParityReplace;
            comboBox7.Text = "" + serilport.RtsEnable;
            comboBox8.Text = "" + serilport.StopBits;
            Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            form1.SerialPort = serilport;
            Close();
        }

        private void comboBox1_TextChanged(object sender, EventArgs e)
        {
            try
            {
                serilport.DataBits = Convert.ToInt32(comboBox1.Text);
            }
            catch (Exception)
            { }
        }

        private void comboBox2_TextChanged(object sender, EventArgs e)
        {
            try
            {
                serilport.DiscardNull = Convert.ToBoolean(comboBox2.Text);
            }
            catch (Exception) { }
        }

        private void comboBox3_TextChanged(object sender, EventArgs e)
        {
            try
            {
                serilport.DtrEnable = Convert.ToBoolean(comboBox3.Text);
            }
            catch (Exception) { }
        }

        private void comboBox4_TextChanged(object sender, EventArgs e)
        {
            try
            {
                string str = comboBox4.Text;
                if (str.Equals(Handshake.None.ToString())) serilport.Handshake = Handshake.None;
                else if (str.Equals(Handshake.RequestToSend.ToString())) serilport.Handshake = Handshake.RequestToSend;
                else if (str.Equals(Handshake.RequestToSendXOnXOff.ToString())) serilport.Handshake = Handshake.RequestToSendXOnXOff;
                else if (str.Equals(Handshake.XOnXOff.ToString())) serilport.Handshake = Handshake.XOnXOff;
            }
            catch (Exception) { }
        }

        private void comboBox5_TextChanged(object sender, EventArgs e)
        {
            try
            {
                string str = comboBox5.Text;
                if (str.Equals(Parity.Even)) serilport.Parity = Parity.Even;
                else if (str.Equals(Parity.Mark)) serilport.Parity = Parity.Mark;
                else if (str.Equals(Parity.None)) serilport.Parity = Parity.None;
                else if (str.Equals(Parity.Odd)) serilport.Parity = Parity.Odd;
                else if (str.Equals(Parity.Space)) serilport.Parity = Parity.Space;
            }
            catch (Exception) { }
        }

        private void comboBox6_TextChanged(object sender, EventArgs e)
        {
            try
            {
                serilport.ParityReplace = Convert.ToByte(comboBox6.Text);
            }
            catch (Exception) { }
        }

        private void comboBox7_TextChanged(object sender, EventArgs e)
        {
            try
            {
                serilport.RtsEnable = Convert.ToBoolean(comboBox7.Text);
            }
            catch (Exception) { }
        }

        private void comboBox8_TextChanged(object sender, EventArgs e)
        {
            try
            {
                string str = comboBox8.Text;
                if (str.Equals(StopBits.None)) serilport.StopBits = StopBits.None;
                else if (str.Equals(StopBits.One)) serilport.StopBits = StopBits.One;
                else if (str.Equals(StopBits.OnePointFive)) serilport.StopBits = StopBits.OnePointFive;
                else if (str.Equals(StopBits.Two)) serilport.StopBits = StopBits.Two;
            }
            catch (Exception) { }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //private void textBox1_TextChanged(object sender, EventArgs e)
        //{
        //    serilport.BaudRate = Convert.ToInt32(textBox1.Text);
        //    //textBox1.Text = serilport.BaudRate.ToString();
        //}
        
        //private void textBox1_TextChanged(object sender, EventArgs e)
        //{

        //    //serilport.BaudRate = Convert.ToInt32(textBox1.Text);
        //    //textBox1.Text = serilport.BaudRate.ToString();
        //}
    }
}